{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3ef80e0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import lightgbm as lgb\n",
    "import xgboost as xgb\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.model_selection import StratifiedKFold,KFold\n",
    "from sklearn.metrics import mean_absolute_error\n",
    "import multiprocessing\n",
    "from sklearn.metrics import f1_score\n",
    "from tqdm import tqdm\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "052782d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>AQI</th>\n",
       "      <th>qua</th>\n",
       "      <th>PM2.5</th>\n",
       "      <th>PM10</th>\n",
       "      <th>SO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>NO2</th>\n",
       "      <th>O3_8h</th>\n",
       "      <th>IPRC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2016/1/1</td>\n",
       "      <td>293</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>243</td>\n",
       "      <td>324</td>\n",
       "      <td>122</td>\n",
       "      <td>6.1</td>\n",
       "      <td>149</td>\n",
       "      <td>12</td>\n",
       "      <td>2.088378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2016/1/2</td>\n",
       "      <td>430</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>395</td>\n",
       "      <td>517</td>\n",
       "      <td>138</td>\n",
       "      <td>7.5</td>\n",
       "      <td>180</td>\n",
       "      <td>18</td>\n",
       "      <td>3.316942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016/1/3</td>\n",
       "      <td>332</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>282</td>\n",
       "      <td>405</td>\n",
       "      <td>72</td>\n",
       "      <td>6.3</td>\n",
       "      <td>130</td>\n",
       "      <td>10</td>\n",
       "      <td>2.516425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2016/1/4</td>\n",
       "      <td>204</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>154</td>\n",
       "      <td>237</td>\n",
       "      <td>73</td>\n",
       "      <td>3.5</td>\n",
       "      <td>72</td>\n",
       "      <td>34</td>\n",
       "      <td>1.505693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2016/1/5</td>\n",
       "      <td>169</td>\n",
       "      <td>中度污染</td>\n",
       "      <td>128</td>\n",
       "      <td>186</td>\n",
       "      <td>99</td>\n",
       "      <td>3.2</td>\n",
       "      <td>66</td>\n",
       "      <td>39</td>\n",
       "      <td>1.210233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>361</th>\n",
       "      <td>2016/12/27</td>\n",
       "      <td>147</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>112</td>\n",
       "      <td>131</td>\n",
       "      <td>33</td>\n",
       "      <td>1.8</td>\n",
       "      <td>60</td>\n",
       "      <td>30</td>\n",
       "      <td>0.889445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>362</th>\n",
       "      <td>2016/12/28</td>\n",
       "      <td>204</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>154</td>\n",
       "      <td>181</td>\n",
       "      <td>33</td>\n",
       "      <td>1.9</td>\n",
       "      <td>65</td>\n",
       "      <td>29</td>\n",
       "      <td>1.346602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>363</th>\n",
       "      <td>2016/12/29</td>\n",
       "      <td>328</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>278</td>\n",
       "      <td>325</td>\n",
       "      <td>71</td>\n",
       "      <td>4.5</td>\n",
       "      <td>96</td>\n",
       "      <td>13</td>\n",
       "      <td>2.305147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>2016/12/30</td>\n",
       "      <td>279</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>229</td>\n",
       "      <td>273</td>\n",
       "      <td>60</td>\n",
       "      <td>3.7</td>\n",
       "      <td>92</td>\n",
       "      <td>11</td>\n",
       "      <td>1.872744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>365</th>\n",
       "      <td>2016/12/31</td>\n",
       "      <td>377</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>327</td>\n",
       "      <td>425</td>\n",
       "      <td>58</td>\n",
       "      <td>6.7</td>\n",
       "      <td>117</td>\n",
       "      <td>9</td>\n",
       "      <td>2.753164</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>366 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           date  AQI   qua  PM2.5  PM10  SO2   CO  NO2  O3_8h      IPRC\n",
       "0      2016/1/1  293  重度污染    243   324  122  6.1  149     12  2.088378\n",
       "1      2016/1/2  430  严重污染    395   517  138  7.5  180     18  3.316942\n",
       "2      2016/1/3  332  严重污染    282   405   72  6.3  130     10  2.516425\n",
       "3      2016/1/4  204  重度污染    154   237   73  3.5   72     34  1.505693\n",
       "4      2016/1/5  169  中度污染    128   186   99  3.2   66     39  1.210233\n",
       "..          ...  ...   ...    ...   ...  ...  ...  ...    ...       ...\n",
       "361  2016/12/27  147  轻度污染    112   131   33  1.8   60     30  0.889445\n",
       "362  2016/12/28  204  重度污染    154   181   33  1.9   65     29  1.346602\n",
       "363  2016/12/29  328  严重污染    278   325   71  4.5   96     13  2.305147\n",
       "364  2016/12/30  279  重度污染    229   273   60  3.7   92     11  1.872744\n",
       "365  2016/12/31  377  严重污染    327   425   58  6.7  117      9  2.753164\n",
       "\n",
       "[366 rows x 10 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.read_csv('data/train.csv',header=0,names=['date','AQI','qua','PM2.5','PM10','SO2','CO','NO2','O3_8h','IPRC'])\n",
    "df2=pd.read_csv('data/test.csv',header=0,names=['date','AQI','qua','PM2.5','PM10','SO2','CO','NO2','O3_8h'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "afd868e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_fea(df):\n",
    "    df['date'] = pd.to_datetime(df['date'], format='%Y/%m/%d')\n",
    "    df[\"month\"]=df[\"date\"].apply(lambda x : x.month)\n",
    "    df[\"day\"]=df[\"date\"].apply(lambda x : x.day)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "cd8a9346",
   "metadata": {},
   "outputs": [],
   "source": [
    "train = get_fea(df)\n",
    "test = get_fea(df2)\n",
    "for col in ['qua']:\n",
    "    le = LabelEncoder()\n",
    "    train[col] = le.fit_transform(train[col])\n",
    "    test[col] = le.transform(test[col])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "01d1870a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每一折验证分数:0.044343326294809096\n",
      "每一折验证分数:0.020716729656543257\n",
      "每一折验证分数:0.02215274308380274\n",
      "每一折验证分数:0.02052453325417224\n",
      "每一折验证分数:0.022610665214254096\n"
     ]
    }
   ],
   "source": [
    "score=[]\n",
    "answers=[]\n",
    "fea = [f for f in train.columns if f not in ['date','IPRC']]\n",
    "# model = lgb.LGBMRegressor(max_depth=15,num_leaves=35,learning_rate=0.03,n_estimators=5000,seed=2020)\n",
    "model = xgb.XGBRegressor(max_depth=7,learning_rate=0.05,n_estimators=10000,subsample=0.8)\n",
    "\n",
    "n_fold = 5\n",
    "folds = KFold(n_splits=n_fold, shuffle=True,random_state=2021)\n",
    "for fold_n, (train_index, valid_index) in enumerate(folds.split(train)):\n",
    "    X_train, X_valid = train[fea].iloc[train_index], train[fea].iloc[valid_index]\n",
    "    y_train, y_valid = train['IPRC'][train_index], train['IPRC'][valid_index]\n",
    "    \n",
    "    model.fit(X_train,y_train,eval_set=[(X_valid, y_valid)],verbose=0,early_stopping_rounds=100)\n",
    "    y_pre=model.predict(X_valid)\n",
    "    print('每一折验证分数:'+str(mean_absolute_error(y_valid,y_pre)))\n",
    "    score = score + mean_squared_error(y_valid,y_pre)\n",
    "    y_pred_valid = model.predict(test[fea])\n",
    "    answers.append(y_pred_valid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0be5175a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2=pd.read_csv('data/test.csv',header=0,names=['date','AQI','qua','PM2.5','PM10','SO2','CO','NO2','O3_8h'])\n",
    "df2['IPRC']=sum(answers)/n_fold\n",
    "df2[['date','IPRC']].to_csv('sub4.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0ccff32",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
